package com.eaoa.base;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.stony.core.bo.HibernateQueryObject;
import com.stony.core.util.CoreUtils;

public class EABaseQuery extends HibernateQueryObject {

	public EABaseQuery(Class clazz) {
		super(clazz);
	}

	protected void setQUERYStockFtcontractcode(String val, String property,
			String querysql) {
		if (StringUtils.isBlank(val))
			return;
		val = StringUtils.replace(val, "'", "''");
		String[] m = val.trim().split(",");
		if (m.length == 0)
			return;

		if (m.length == 1) {
			String str = querysql + " LIKE '%" + m[0] + "%' ";
			setQUERYCondition(property, str);
			return;
		}
		String ret = "";
		for (int i = 0; i < m.length; i++) {
			if (i == 0)
				ret += "'" + m[i] + "'";
			else
				ret += ", '" + m[i] + "'";
		}
		String str = querysql + " IN ( " + ret + ") ";
		setQUERYCondition(property, str);
	}

	protected void setCHILDQUERYString(String val, String property, String hql,
			String parameter1) {
		if (StringUtils.isBlank(val))
			return;
		setCHILDQUERYCondition(property, hql, val, null, parameter1, null);
	}

	protected void setCHILDQUERYDate(String dateBegin, String dateEnd,
			String property, String hql1, String hql2, String hql3,
			String parameter1, String parameter2) {
		if (StringUtils.isNotBlank(dateBegin)) {
			if (StringUtils.isBlank(dateEnd)) {
				setCHILDQUERYCondition(property, hql2, CoreUtils
						.parseDate(dateBegin), null, parameter1, parameter2);
			} else {
				setCHILDQUERYCondition(property, hql1, CoreUtils
						.parseDate(dateBegin), CoreUtils.nextDate(DateUtils
						.truncate(CoreUtils.parseDate(dateEnd),
								Calendar.DAY_OF_MONTH)), parameter1, parameter2);
			}
		} else {

			if (StringUtils.isNotBlank(dateEnd))

				setCHILDQUERYCondition(property, hql3, null, CoreUtils
						.parseDate(dateEnd), parameter1, parameter2);
		}
	}

	public void setOrder(String sorttype, String sortfield) {
		this.setPageOrder(sorttype, sortfield);
	}
}
